package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingPlanDto;
import com.zzyl.dto.NursingProjectPlanDto;
import com.zzyl.entity.NursingPlan;
import com.zzyl.entity.NursingProjectPlan;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.NursingPlanMapper;
import com.zzyl.service.NursingPlanService;
import com.zzyl.vo.NursingPlanVo;
import com.zzyl.vo.NursingProjectPlanVo;
import com.zzyl.vo.NursingProjectVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class NursingPlanServiceImpl implements NursingPlanService {

    @Autowired
    private NursingPlanMapper nursingPlanMapper;


    @Override
    public List<NursingPlanVo> selectAll() {
        return nursingPlanMapper.selectAll();
    }

    @Override
    public PageResponse<NursingPlanVo> selectByPage(Integer pageNum, Integer pageSize, String name, Integer status) {
        PageHelper.startPage(pageNum, pageSize);
        Page<NursingPlanVo> page = nursingPlanMapper.selectPageByNameAndStatus(name, status);


        return PageResponse.of(page, NursingPlanVo.class);
    }

    @Override
    @Transactional
    public int addNursingPlan(NursingPlanDto nursingPlanDto) {

        NursingPlan nursingPlan = BeanUtil.copyProperties(nursingPlanDto, NursingPlan.class);
        nursingPlan.setStatus(0);
        //校验计划名称
        int row=nursingPlanMapper.countByPlanName(nursingPlan.getPlanName());
        if(row>0){
            throw new BaseException(BasicEnum.INSERT_NURSINGPLAN_FAIL);
        }
        int affects= nursingPlanMapper.insert(nursingPlan);
        List<NursingProjectPlanDto> projectPlans = nursingPlanDto.getProjectPlans();
        projectPlans.forEach(s->{
            s.setPlanId(nursingPlan.getId());
        });
        List<NursingProjectPlan> nursingProjectPlans = BeanUtil.copyToList(projectPlans, NursingProjectPlan.class);
        nursingPlanMapper.insertProjectPlan(nursingProjectPlans);
        return affects;

    }

    @Override
    public NursingPlanVo selectById(Long id) {
        NursingPlanVo nursingPlanVo = nursingPlanMapper.selectById(id);
        List<NursingProjectPlanVo> projectPlanVos= nursingPlanMapper.selectProjectPlanById(id);
        nursingPlanVo.setProjectPlans(projectPlanVos);

        return nursingPlanVo;

    }

    @Transactional
    @Override
    public int updateNursingPlan(NursingPlanDto nursingPlanDto) {
        //校验计划名称
        String planName = nursingPlanDto.getPlanName();
        if(!planName.equals(nursingPlanMapper.selectById(nursingPlanDto.getId()).getPlanName())){
            int row=nursingPlanMapper.countByPlanName(planName);
            if(row>0){
                throw new BaseException(BasicEnum.INSERT_NURSINGPLAN_FAIL);
            }
        }
        NursingPlan nursingPlan = BeanUtil.copyProperties(nursingPlanDto, NursingPlan.class);
        int affects = nursingPlanMapper.updateNursingPlan(nursingPlan);
        if(nursingPlanDto.getProjectPlans()!=null) {
            nursingPlanMapper.deleteProjectPlan(nursingPlan.getId());
        }
        List<NursingProjectPlan> nursingProjectPlans = BeanUtil.copyToList(nursingPlanDto.getProjectPlans(), NursingProjectPlan.class);
        nursingProjectPlans.forEach(s->s.setPlanId(nursingPlan.getId()));
        nursingPlanMapper.insertProjectPlan(nursingProjectPlans);


        return affects;
    }

    @Transactional
    @Override
    public int deleteById(Long id) {
        if(nursingPlanMapper.selectProjectPlanById(id)!=null) {
            nursingPlanMapper.deleteProjectPlan(id);
        }
        return nursingPlanMapper.deleteById(id);
    }

    @Override
    public int updateNursingPlanStatus(Long id, Integer status) {
        return nursingPlanMapper.updateNursingPlanStatus(id,status);
    }


}
